﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cxx.Common;
using Cxx.Entity;
using Cxx.Entity.Extend;
using Cxx.IDAL;
using Dapper;

namespace Cxx.DAL
{
    public class CusTotalLogs : DataAccess, ICusTotalLogs
    {
        public void Insert(CusTotalLogsInfo info)
        {
            const string sql =
                "INSERT INTO tblCusTotalLogs (Receiver,Amount,SelectTime,OperateTime) VALUES(@Receiver,@Amount,@SelectTime,@OperateTime)";
            using (IDbConnection dbConnection = GetConnection())
            {
                dbConnection.Execute(sql, info);
            }
        }

        /// <summary>
        /// 分页获取列表
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <returns></returns>
        public List<CusTotalLogsInfo> GetListForPage(string condition, int pageIndex, int pageSize, out int recordCount)
        {
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("@_tables", "tblCusTotalLogs", DbType.String, null, 1000);
            //parameters.Add("@PK", "Id", DbType.String, null, 100);
            parameters.Add("@_orderby", "Id DESC", DbType.String, null, 200);
            parameters.Add("@_pageindex", pageIndex, DbType.Int32);
            parameters.Add("@_pageSize", pageSize, DbType.Int32);
            parameters.Add("@_fields", "*", DbType.String, null, 1000);

            #region 获取查询条件
            Hashtable fields = new Hashtable();
            var field = new DataFieldInfo("OperateTime", "DateTime", "tblOrder", ">=");
            fields.Add("txtStartTime", field);
            field = new DataFieldInfo("OperateTime", "DateTime", "tblOrder", "<=");
            fields.Add("txtEndTime", field);
            field = new DataFieldInfo("Receiver", "Character", "tblOrder", "=");
            fields.Add("txtCustomerName", field);

            #endregion
            string sqlWhere = SqlFilter.Build(condition, fields);

            parameters.Add("@_where", sqlWhere, DbType.String, null, 1000);
            parameters.Add("@_totalcount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            parameters.Add("@_pagecount", dbType: DbType.Int32, direction: ParameterDirection.Output);
            using (IDbConnection dbConnection = GetConnection())
            {
                var result = dbConnection.Query<CusTotalLogsInfo>(USP_PAGING_PROCEDURE, parameters,
                    commandType: CommandType.StoredProcedure);
                if (result == null)
                {
                    recordCount = 0;
                    return null;
                }
                recordCount = parameters.Get<int>("@_totalcount");
                return result.ToList();
            }
        }

        public void Delete(int id)
        {
            const string sql = "DELETE FROM tblCusTotalLogs WHERE Id=@Id";
            object para = new {Id = id};
            using (IDbConnection dbConnection = GetConnection())
            {
                dbConnection.Execute(sql, para);
            }
        }
    }
}
